<template>
  <div class="Admin_news">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>热点消息</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
        <el-row :gutter="20">
          <el-col :span="2"><el-button type="primary" @click="adddialogVisible = true" class="add_btn">添加热点消息</el-button></el-col>
        </el-row>
        <el-table :data="NewsList" style="width: 100%" border stripe>
          <el-table-column type="index"></el-table-column>
          <el-table-column prop="title" label="标题"></el-table-column>
          <el-table-column prop="author" label="作者"></el-table-column>
          <el-table-column label="内容" width="300px">
            <template slot-scope="scope">
              <p class="newsContent">{{scope.row.content}}</p>
            </template>
          </el-table-column>
          <el-table-column prop="agree" label="点赞数"></el-table-column>
          <el-table-column label="操作" width="200px">
            <template slot-scope="scope">
              <el-button type="primary" icon="el-icon-edit" size="mini" @click="showeditForm(scope.row.id)"></el-button>
              <el-button type="danger" icon="el-icon-delete" size="mini" @click="deleteNews(scope.row.id)" ></el-button>
            </template>
          </el-table-column>
        </el-table>
    </el-card>

    <el-dialog title="添加消息" :visible.sync="adddialogVisible" width="40%" @close="resetAdd">
      <el-form :model="addForm" :rules="addrules" ref="addFormRef" label-width="70px">
        <el-form-item label="标题" prop="title">
          <el-input v-model="addForm.title"></el-input>
        </el-form-item>
        <el-form-item label="作者" prop="author">
          <el-input v-model="addForm.author"></el-input>
        </el-form-item>
        <el-form-item label="内容" prop="content">
          <el-input type="textarea" v-model="addForm.content"></el-input>
        </el-form-item>
        <el-form-item label="点赞数" prop="agree">
          <el-input v-model="addForm.agree"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
    <el-button @click="adddialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="addNews">确 定</el-button>
  </span>
    </el-dialog>
    <el-dialog title="修改消息" :visible.sync="editdialogVisible" width="30%" @close="resetedit">
      <el-form :model="editForm" :rules="editrules" ref="editFormRef" label-width="70px">
        <el-form-item label="标题" prop="title">
          <el-input v-model="editForm.title"></el-input>
        </el-form-item>
        <el-form-item label="作者" prop="author">
          <el-input v-model="editForm.author"></el-input>
        </el-form-item>
        <el-form-item label="内容" prop="content">
          <el-input v-model="editForm.content"></el-input>
        </el-form-item>
        <el-form-item label="点赞数" prop="agree">
          <el-input v-model="editForm.agree"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
    <el-button @click="editdialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="editNews">确 定</el-button>
  </span>
    </el-dialog>
  </div>
</template>

<script>

import {mapState} from "vuex";

export default {
  name: "news",
  data(){
    return{
      adddialogVisible:false,
      addForm:{
        title:'',
        author:'',
        content:'',
        agree:'',
      },
      addrules:{
        title:[{required:true,message:'请输入标题',trigger:'blur'},
          { min: 3, max: 10, message: '标题在3~10个字符之间', trigger: 'blur' }],
        author: [{required:true,message:'作者不能为空',trigger:'blur'}],
        content:[{required:true,message:'请输入内容',trigger:'blur'}],
        agree:[]
      },
      editdialogVisible:false,
      editForm:{
        title:'',
        author:'',
        content:'',
        agree:'',
      },
      editrules:{
        title:[{required:true,message:'请输入标题',trigger:'blur'},
          { min: 3, max: 10, message: '标题在3~10个字符之间', trigger: 'blur' }],
        author: [{required:true,message:'作者不能为空',trigger:'blur'}],
        content:[{required:true,message:'请输入内容',trigger:'blur'}],
        agree:[]
      },
    }
  },
  computed:{...mapState({
      NewsList:state=>state.home.NewsList,
    })},
  beforeCreate() {
    this.$store.dispatch('home/getNewsList')
  },
  methods:{
    resetAdd(){
      this.$refs.addFormRef.resetFields();
    },
    addNews(){
      this.$refs.addFormRef.validate( async (valid) => {
        if (valid) {
          const res = await this.$http.post('/api/news/add', this.addForm)
          if (res.data.status !== 200) {
            return this.$message.error('添加失败')
          }
          this.$message.success('添加成功')
          this.adddialogVisible = false
          this.$store.dispatch('home/getNewsList')
        } else {
          return this.$message.error('请填写完整的信息')
        }
      })
    },
    async showeditForm(id){
      const res=await this.$http.get('/api/news/search?id='+id)
      if(res.data.status!==200){return this.$message.error('获取信息失败')}
      else{
        this.editForm = res.data.data;
        this.editdialogVisible = true;
      }

    },
    resetedit(){
      this.$refs.editFormRef.resetFields();
    },
    editNews(){
      this.$refs.editFormRef.validate(async valid=>{
        if(valid){
          const resedit=await this.$http.put('/api/news/edit?id='+this.editForm.id,this.editForm)
          if(resedit.data.status!==200){
            this.$message.error('修改信息失败')
          }else {
            this.$message.success('修改信息成功')
            this.editdialogVisible=false
            this.$store.dispatch('home/getNewsList')
          }
        }else {
          return this.$message.error('请填写完整信息')
        }
      })
    },
    async deleteNews(id){
      const delres=await this.$confirm('是否删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err=>err);
      if(delres=="confirm"){
        const res=await this.$http.delete('api/news/del?id='+id)
        if(res.data.code!==1){this.$message.error('删除失败')}
        else{this.$message.success('删除成功')
          this.$store.dispatch('home/getNewsList')}
      }else {
        this.$message.error('已经取消删除')
      }
    }
  }

}
</script>

<style scoped lang="less">
.Admin_news{
.add_btn{
  margin-bottom: 20px;
}
.newsContent{
  font-size: 17px;
  color: #656262;
  text-overflow: ellipsis;
  overflow: hidden;
  white-space: nowrap;
}
}
</style>
